<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>用户信息管理系统</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="../../css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="../../js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="../../js/bootstrap.min.js"></script>
    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>

    <script>
        function deleteUser(id) {
            //提示
            if (confirm("你确定要删除吗？")){
                location.href="${pageContext.request.contextPath}/user/deleteById?id="+id;
            }
        }

        window.onload = function () {
            //js复选框  全选
            document.getElementById("mother").onclick = function () {
                var elementById =  document.getElementsByName("uid");
                for (var i = 0; i <= elementById.length; i++) {
                    elementById[i].checked = this.checked;
                }
            }
        }

        //批量删除事件
        function checkCounts() {
            if (confirm("你确定要删除吗？")){
                document.getElementById("form").submit();
            }
        }

    </script>
</head>
<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>
    <%--表单  动态查询  --%>
    <form class="form-inline" action="${pageContext.request.contextPath}/user/findByPage" method="post">
        <div class="form-group">
            <label for="name">姓名</label>
            <input type="text" class="form-control" id="name" name="name" value="${map.name}" >
        </div>
        <div class="form-group">
            <label for="address">籍贯</label>
            <input type="text" class="form-control" id="address" name="address" value="${map.address}" >
        </div>
        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" class="form-control" id="email" name="email" value="${map.email}" >
        </div>
        <button type="submit" class="btn btn-default">查询</button>
    </form>
    <div style="float: right;margin: 5px">
        <a class="btn btn-primary" href="${pageContext.request.contextPath}/user/toAdd" >添加联系人</a>
        <td colspan="8" align="center"><a class="btn btn-primary" onclick="checkCounts()">删除选中</a></td><%--要把表格丢到form表单里--%>
    </div>

    <form id="form" action="${pageContext.request.contextPath}/user/deleteCounts" method="post">
        <table border="1" class="table table-bordered table-hover">
            <tr class="success">
                <th><input type="checkbox" id="mother"></th><%--这个id就是一点击就全选--%>
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>籍贯</th>
                <th>技能</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>

            <c:forEach items="${pageInfo.list}" var="user" varStatus="u">
                <tr>
                    <td><input type="checkbox" name="uid" value="${user.id}"></td>
                    <td>${u.count}</td>
                    <td>${user.name}</td>
                    <td>${user.gender}</td>
                    <td>${user.age}</td>
                    <td>${user.address}</td>
                    <td>${user.art}</td>
                    <td>${user.email}</td>
                    <td>
                        <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/user/findById?id=${user.id}">修改</a>&nbsp;
                        <a class="btn btn-default btn-sm" href="javaScript:deleteUser(${user.id})">删除</a>
                            <%--href="javaScript:deleteUser(${user.id})是一个方法--%>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </form>
    <%--分页--%>
    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <%--判断当前页是否是第一页，决定左箭头是否禁用--%>
                <li>
                    <c:if test="${pageInfo.pageNum==1}">

                    </c:if>
                    <c:if test="${pageInfo.pageNum != 1}">
                        <a href="${pageContext.request.contextPath}/user/findByPage?pageNum=${pageInfo.pageNum-1}&pageSize=${pageInfo.pageSize}&name=${map.name}&address=${map.address}&email=${map.email}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </c:if>
                </li>


                <%--遍历总页数  显示 1  2   3  4--%>
                <c:forEach begin="1" end="${pageInfo.pages}" var="i">
                    <%--判断当前循环的数字是否与当前页数的数字相同，相同加上蓝色背景属性--%>
                    <c:if test="${pageInfo.pageNum == i}">
                        <li class="active">
                            <a href="${pageContext.request.contextPath}/user/findByPage?pageNum=${i}&pageSize=${pageInfo.pageSize}&name=${map.name}&address=${map.address}&email=${map.email}">${i}</a>
                        </li>
                    </c:if>

                    <c:if test="${pageInfo.pageNum != i}">
                        <li>
                            <a href="${pageContext.request.contextPath}/user/findByPage?pageNum=${i}&pageSize=${pageInfo.pageSize}&name=${map.name}&address=${map.address}&email=${map.email}">${i}</a>
                        </li>
                    </c:if>

                </c:forEach>

                <%--判断当前页是否是最后一页，决定是否右箭头禁用--%>
                <li>
                    <c:if test="${pageInfo.pageNum == pageInfo.pages}">

                    </c:if>
                    <c:if test="${pageInfo.pageNum != pageInfo.pages}">
                        <a href="${pageContext.request.contextPath}/user/findByPage?pageNum=${pageInfo.pageNum+1}&pageSize=${pageInfo.pageSize}&name=${map.name}&address=${map.address}&email=${map.email}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </c:if>
                </li>

                <span style="font-size:24px;margin-left: 5px">
                    共<span style="color: red">${pageInfo.total}</span>条记录,共<span style="color: red">${pageInfo.pages}</span>页
                </span>
            </ul>
        </nav>
    </div>
</div>
</body>
</html>
